package com.wm.task.dao;

import com.wm.task.util.DbHelper;
import com.wm.task.vo.AdminUser;
import com.wm.task.vo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class BeginDAO {
    private User user;
    private AdminUser adminUser;

    public void register(String phoneNum, String pass, String name, String secretQuestion, String secretAnswer) {
        String sql = "INSERT INTO user (phone_num, pass,name,secret_question,secret_answer) VALUES (?,?,?,?,?);";
        DbHelper.update(sql, phoneNum, pass, name, secretQuestion, secretAnswer);
    }


    public User userLogin(String phoneNum, String pass) {
        String sql = "SELECT * FROM user WHERE phone_num = ? AND pass = ?";
        Connection connection = DbHelper.getDate();
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1, phoneNum);
            ps.setObject(2, pass);
            ResultSet rs = ps.executeQuery();
            rs.next();
            user = new User(rs.getInt("id"), rs.getString("phone_num"), rs.getString("pass"),
                    rs.getString("name"), rs.getTime("time"), rs.getString("secret_question"), rs.getString("secret_answer"));

            DbHelper.close(connection);
        } catch (Exception e) {
            System.out.println("登录错误");

        }
        return user;
    }

    public AdminUser adminLogin(String account, String pass) {
        String sql = "SELECT * FROM admin WHERE account = ? AND pass = ?";
        Connection connection = DbHelper.getDate();
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1, account);
            ps.setObject(2, pass);
            ResultSet rs = ps.executeQuery();
            rs.next();
            adminUser = new AdminUser(rs.getInt("id"), rs.getString("account"), rs.getString("pass"));

            DbHelper.close(connection);
        } catch (Exception e) {
            System.out.println("登录错误");

        }
        return adminUser;
    }

    public User verPhoneNum(String phoneNum) {

        String sql = "SELECT * FROM user WHERE phone_num = ?";
        Connection connection = DbHelper.getDate();
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1, phoneNum);
            ResultSet rs = ps.executeQuery();
            rs.next();
            user = new User(rs.getInt("id"), rs.getString("phone_num"), rs.getString("pass"),
                    rs.getString("name"), rs.getTime("time"), rs.getString("secret_question"), rs.getString("secret_answer"));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败");
        } finally {
            DbHelper.close(connection);
        }
        return user;

    }

    public void resetPassword(User user, String pass) {
        //UPDATE user SET pass = ? WHERE phone_num = ?;
        String sql = "UPDATE user SET pass = ? WHERE phone_num = ?;";
        DbHelper.update(sql, pass, user.getPhoneNum());
    }


}
